import _service from '@/service/index'
import { ElMessage } from "element-plus"
const carBox = {
    state () {
        return {
            cart: JSON.parse(sessionStorage.getItem('key')) || [],
            allFlag: true,
            goodLts: []
        }
    },
    getters: {
        total (state) {
            return state.cart.length
        },
        allchange (state) {
            return state.allFlag
        },
        cart (state) {
            return state.cart
        },
        //计算选中总数量
        checkTotal (state) {
            let c = 0
            c += state.cart.filter(y => y.isCheck).reduce((total, item) => total += item.amount,
                0)
            console.log(c)
            return c
        },
        //计算选中的总价格
        checkedGoodsAmount (state) {
            let c = 0
            c += state.cart.filter(y => y.isCheck).reduce((total, item) => total += item.amount * item.price,
                0)
            return c
        }
    },
    mutations: {
        changeGoods (state, data) {
            state.goodLts = data
        },
        //添加购物车
        change_cart (state, data) {
            //debugger
            // state.cart.forEach(y => y.isCheck = state.allFlag)

            if (state.cart.length == 0) {
                state.cart.push(data)
            } else {
                if (state.cart.findIndex(item => item.gid == data.gid) != -1) {
                    state.cart.map(item => {
                        if (item.gid == data.gid) {
                            item.amount = item.amount + 1
                            item.shopAmount = item.amount
                        }
                    })

                } else {
                    state.cart.push(data)
                }
            }
            if (state.cart.length > 0) {
                debugger
                state.goodLts.forEach((item) => {
                    state.cart.forEach((obj) => {
                        if (item.gid == obj.gid) {
                            item.isAdd = true
                            item.shopAmount = obj.amount
                        }
                    })
                })
            }
            sessionStorage.removeItem('key')
            this.commit('carBox/saveCarLts', 'key')
            this.commit('carBox/updateAllFlag')
        },
        //根据gid删除购物车
        remove (state, gid) {
            // debugger

            state.cart.forEach((item, index) => {
                if (item.gid == gid) {
                    state.cart.splice(index, 1)
                }
            })
            if (state.cart.length > 0) {
                state.goodLts.forEach((item) => {
                    state.cart.forEach((obj) => {
                        if (item.gid == obj.gid) {
                            item.isAdd = true
                        }
                    })
                })
            }
            sessionStorage.removeItem('key')
            this.commit('carBox/saveCarLts', 'key')

        },
        //根据是否选中清除购物车数据
        removeIsCheck (state) {
            // debugger
            state.cart = state.cart.filter(item => item.isCheck == false)

            if (state.cart.length > 0) {
                state.goodLts.forEach((item) => {
                    state.cart.forEach((obj) => {
                        if (item.gid == obj.gid) {
                            item.isAdd = true
                        }
                    })
                })
            }
            sessionStorage.removeItem('key')
            this.commit('carBox/saveCarLts', 'key')
            this.commit('carBox/updateAllFlag')
        },
        //一键清空购物车
        delectAll (state) {
            state.cart = []
            sessionStorage.removeItem('key')
        },
        remove2 (state, gid) {
            state.cart.forEach((item, index) => {
                if (item.gid == gid) {
                    item.amount = item.amount - 1
                }
            })

            sessionStorage.removeItem('key')
            this.commit('carBox/saveCarLts', 'key')
        },
        ///保存本地
        saveCarLts (state) {
            sessionStorage.setItem('key', JSON.stringify(state.cart))
        },
        ///全选/反选
        updateAllGoodsState (state) {
            // debugger
            state.allFlag = !state.allFlag
            state.cart.forEach(y => y.isCheck = state.allFlag)
        },
        //修改全选反选状态
        updateAllFlag (state) {
            const ltsTrue = []
            if (state.cart.length > 0) {
                state.cart.forEach(x => {
                    ltsTrue.push(x.isCheck)
                })
                if (ltsTrue.indexOf(false) != -1) {
                    state.allFlag = false
                } else {
                    state.allFlag = true
                }
            } else {
                state.allFlag = false
            }

        }
    },
    actions: {
        async isAddShop ({ commit, state }, value) {
            const res = await _service.isAddShop(value.gid)
            debugger
            if (res.errCode == 0) {
                if (!res.data) {
                    ElMessage({
                        message: "该商品不存在!",
                        type: "error",
                    })
                    return false
                }
                if (res.data.IsLimited) {
                    if (res.data.Amount > 0) {
                        if (state.cart.length == 0) {
                            this.commit('carBox/change_cart', value)
                        } else {

                            const arr = state.cart.filter(item => item.gid == value.gid)
                            if (arr.length == 0) {
                                this.commit('carBox/change_cart', value)
                            } else {
                                if (arr[0].amount < res.data.Amount) {
                                    this.commit('carBox/change_cart', value)
                                } else {
                                    ElMessage({
                                        message: "超出数量限制",
                                        type: "error",
                                    })
                                }
                            }
                        }
                    } else {
                        ElMessage({
                            message: "该商品限购限制",
                            type: "error",
                        })
                    }

                    return false

                } else {
                    commit('carBox/change_cart', value)
                }
            }
            console.log(11, res)
        }
    },
    namespaced: true
}

export default carBox